package com.nciae.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nciae.dto.OrderDTO;
import com.nciae.exception.SellException;
import com.nciae.po.OrderMaster;
import com.nciae.po.ProductInfo;
import com.nciae.service.IOrderMasterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author YangChen
 * @since 2021-10-14
 */
@Controller
@RequestMapping("/orderMaster")
public class OrderMasterController {

    @Autowired
    private IOrderMasterService orderMasterService;

    @PostMapping
    public ResponseEntity<OrderMaster> Insert(@RequestBody OrderMaster orderMaster) {
        return ResponseEntity.ok(orderMasterService.Insert(orderMaster));
    }


    @GetMapping("/list")
    public ModelAndView list(@RequestParam(value = "page" ,defaultValue = "0") Integer page,
                             @RequestParam(value = "size" ,defaultValue = "5")Integer size,
                             Map<String, Object> map
    ){
        IPage<OrderDTO> pages = orderMasterService.findByPage(page, size);
        map.put("orderDTOList", pages.getRecords());
        map.put("total", pages.getPages());
        map.put("page",pages.getCurrent());
        map.put("size", pages.getSize());
        return new ModelAndView("/order/list",map);
    }

    @GetMapping("/cancel")
    public ModelAndView cancel(@RequestParam(value = "orderId") String orderId,
                               @RequestParam(value = "page" ,defaultValue = "0") Integer page,
                               @RequestParam(value = "size" ,defaultValue = "5")Integer size,
                               Map<String, Object> map
                               ) throws SellException {

        OrderDTO orderDTO = orderMasterService.findOne(orderId);
        orderMasterService.cancel(orderDTO);

        IPage<OrderDTO> pages = orderMasterService.findByPage(page, size);
        map.put("orderDTOList", pages.getRecords());
        map.put("total", pages.getPages());
        map.put("page",pages.getCurrent());
        map.put("size", pages.getSize());

        return new ModelAndView("/order/list", map);
    }


    @GetMapping("/detail")
    public ModelAndView detail(@RequestParam("orderId") String orderId,
                               @RequestParam(value = "page" ,defaultValue = "0") Integer page,
                               @RequestParam(value = "size" ,defaultValue = "5")Integer size,
                               Map<String, Object> map
                               ) throws SellException {
        OrderDTO orderDTO = orderMasterService.findOne(orderId);
        map.put("orderDTO", orderDTO);
        map.put("page", page);
        map.put("size", size);
        return new ModelAndView("/order/detail", map);
    }



    @GetMapping("/finish")
    public ModelAndView finish(@RequestParam(value = "orderId") String orderId,
                               @RequestParam(value = "page" ,defaultValue = "0") Integer page,
                               @RequestParam(value = "size" ,defaultValue = "5")Integer size,
                               Map<String, Object> map
    ) throws SellException {

        OrderDTO orderDTO = orderMasterService.findOne(orderId);
        orderMasterService.finish(orderDTO);

        IPage<OrderDTO> pages = orderMasterService.findByPage(page, size);
        map.put("orderDTOList", pages.getRecords());
        map.put("total", pages.getPages());
        map.put("page",pages.getCurrent());
        map.put("size", pages.getSize());

        return new ModelAndView("/order/list", map);
    }


}
